<?php

App::$app->addRoute('GET /', 'app\DefaultController->actionIndex');
App::$app->addRoute('GET /dialog.php', function($f3){
            $action = new \app\DefaultController();
            if($action->beforeRoute()){
            	return $action->actionIndex();
            }
            die();
        });
App::$app->addRoute('POST /upload', 'app\DefaultController->actionUpload');
// App::$app->addRoute('GET /test', 'app\DefaultController->test');

/**
* Convert convert size in bytes to human readable
*
* @param  int  $size
*
* @return  string
*/
function makeSize($size)
{
	$units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
	$u = 0;
	while ((round($size / 1024) > 0) && ($u < 4))
	{
		$size = $size / 1024;
		$u++;
	}

	return (number_format($size, 0) . " " . $units[ $u ]);
}
function sanitize($str)
{
	return strip_tags(htmlspecialchars($str));
}
function fix_dirname($str)
{
	return str_replace('~', ' ', dirname(str_replace(' ', '~', $str)));
}
function fix_filename($str, $config = [], $is_folder = false)
{
	$str = sanitize($str);
	
	if (!mb_detect_encoding($str, 'UTF-8', true))
	{
		$str = utf8_encode($str);
	}

	if (function_exists('transliterator_transliterate'))
	{
		$str = transliterator_transliterate('Any-Latin; Latin-ASCII', $str);
	}
	else
	{
		$str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
	}

	$str = preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $str);

	$str = str_replace(array( '"', "'", "/", "\\" ), "", $str);
	$str = strip_tags($str);
	// Empty or incorrectly transliterated filename.
	// Here is a point: a good file UNKNOWN_LANGUAGE.jpg could become .jpg in previous code.
	// So we add that default 'file' name to fix that issue.
	if (strpos($str, '.') === 0 && $is_folder === false)
	{
		$str = 'file' . date('ymdis') . $str;
	}

	return trim($str);
}
/**
* Cleanup input
*
* @param  string  $str
*
* @return  string
*/
function fix_get_params($str)
{
	return empty($str) ? '' : strip_tags(preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $str));
}

$descending = 1;
function filenameSort($x, $y)
{
    global $descending;

    if ($x['is_dir'] !== $y['is_dir']) {
        return ($descending) ? ($x['is_dir'] <= $y['is_dir'] ? 1 : -1)
        : ($x['is_dir'] > $y['is_dir'] ? 1 : -1);
    } else {
        return ($descending)
            ? ($x['file_lcase'] < $y['file_lcase'] ? 1: -1)
            : ($x['file_lcase'] >= $y['file_lcase'] ? 1 : -1);
    }
}

function dateSort($x, $y)
{
    global $descending;

    if ($x['is_dir'] !== $y['is_dir']) {
        return ($descending) ? ($x['is_dir'] <= $y['is_dir'] ? 1 : -1)
        : ($x['is_dir'] > $y['is_dir'] ? 1 : -1);
    } else {
        return ($descending)
            ?  ($x['date'] < $y['date'] ? 1: -1)
            :  ($x['date'] >= $y['date'] ? 1: -1);
    }
}

function sizeSort($x, $y)
{
    global $descending;

    if ($x['is_dir'] !== $y['is_dir']) {
        return ($descending) ? ($x['is_dir'] <= $y['is_dir'] ? 1 : -1)
        : ($x['is_dir'] > $y['is_dir'] ? 1 : -1);
    } else {
        return ($descending)
            ? ($x['size'] < $y['size'] ? 1: -1)
            : ($x['size'] >= $y['size'] ? 1: -1);
    }
}

function extensionSort($x, $y)
{
    global $descending;

    if ($x['is_dir'] !== $y['is_dir']) {
        return ($descending) ? ($x['is_dir'] <= $y['is_dir'] ? 1 : -1)
        : ($x['is_dir'] > $y['is_dir'] ? 1 : -1);
    } else {
        return ($descending)
            ? ($x['extension'] < $y['extension'] ? 1 : -1)
            : ($x['extension'] >= $y['extension'] ? 1 : -1);
    }
}